package com.szqhyxj.youxiangju.sys.util;

import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;
import android.widget.TextView;

import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;


public class StringUtils {
    public static final String RMB = "￥";

    public static boolean isEmpty(String string) {
        boolean ret = true;
        if (string != null && string.length() != 0) {
            //有一个字符不为空，那么整个字符串就非空了
            for (int i = 0; i < string.length(); i++) {
                if (!Character.isWhitespace(string.charAt(i))) {
                    ret = false;
                    break;
                }
            }
        }
        return ret;
    }

    public static String getMD5(String source) {
        String s = null;
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};// 用来将字节转换成16进制表示的字符
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            md.update(source.getBytes());
            byte tmp[] = md.digest();// MD5 的计算结果是一个 128 位的长整数，  
            // 用字节表示就是 16 个字节  
            char str[] = new char[16 * 2];// 每个字节用 16 进制表示的话，使用两个字符， 所以表示成 16  
            // 进制需要 32 个字符  
            int k = 0;// 表示转换结果中对应的字符位置  
            for (int i = 0; i < 16; i++) {// 从第一个字节开始，对 MD5 的每一个字节// 转换成 16  
                // 进制字符的转换  
                byte byte0 = tmp[i];// 取第 i 个字节  
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];// 取字节中高 4 位的数字转换,// >>>  
                // 为逻辑右移，将符号位一起右移  
                str[k++] = hexDigits[byte0 & 0xf];// 取字节中低 4 位的数字转换  

            }
            s = new String(str);// 换后的结果转换为字符串

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return s;
    }

    public static String[] convertStrToArray(String update_mode) {
        String[] versionStrings = update_mode.split(",");
        return versionStrings;
    }

    public static String double2Moeny(double price) {
        return new String().format("%s%.2f", RMB, price);
    }

    public static String double2(double price) {
        return new String().format("%.2f", price);
    }

    public static String double2NoDecimal(double price) {
        return new String().format("%.0f", price);
    }

    public static String float2Decimal(float price) {
        return new String().format("%.1f", price);
    }

    public static String double2MoneyFormat(double price) {
        return new String().format("%s%.0f", RMB, price);
    }

    public static String double2String(double price) {
        return new String().format("%.2f%s", price, "元");
    }

    public static boolean isNumeric(String str) {
        if (str.isEmpty()) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    public static String int2Decimal(int price) {
        return new String().format("%.1f", price / 100.0f);
    }

    /**
     * 不同颜色文本
     * @param colorL
     * @param txtL
     * @param colorR
     * @param txtR
     * @return
     */
    public static void setColorText(TextView tv, int colorL, String txtL, int colorR, String txtR) {
        String str = "<font color=\'" + colorL + "\'>" + txtL + "</font><font color=\'" + colorR + "\'>\t\t" + txtR + "</font>";
        tv.setText(Html.fromHtml(str));
    }

    public static void setColorTxt(Context context, TextView tv, int colorL, int sizeL, String txtL, int colorR, int sizeR, String txtR) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        SpannableString span_one = new SpannableString(txtL);
        span_one.setSpan(new ForegroundColorSpan(colorL), 0, txtL.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span_one.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(context, sizeL)), 0, txtL.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString span_two = new SpannableString(txtR);
        span_two.setSpan(new ForegroundColorSpan(colorR), 0, txtR.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        span_two.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(context, sizeR)), 0, txtR.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.append(span_one).append(span_two);
        tv.setText(builder);
    }




    /**
     * 替换指定位置的字符串
     *
     * @param end 替换结束位置，不包含关系
     */
    public static String replaceIndexStr(String content, String replace, int start, int end) {
        StringBuilder builder = new StringBuilder(content);
        builder.replace(start, end, replace);
        return builder.toString();
    }

    public static String initPrice(String price){
        StringBuffer buffer = new StringBuffer();
        buffer.append(price);
        int leng = price.length();
        for(int i=leng%3>0?leng/3-1:leng/3-2;i>0;i--){
            buffer.insert(leng%3>0?(i)*3-(3-leng%3):i*3 ,",");
        }
        return buffer.toString();
    }
    public static void setEditTextHintSize(Context mContext,EditText editText, String hintText, int size){
        SpannableString ss = new SpannableString(hintText);
        //定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(DensityUtil.dp2px(mContext,size));
        //设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); editText.setHint(new SpannedString(ss));
    }

}
